package com.yang.mall;

import com.yang.mall.consts.MallConst;
import com.yang.mall.exception.UserLoginException;
import com.yang.mall.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
// 拦截器是基于url进行工作的，我们可以对拦截器进行配置，这个在配置类
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user =(User) request.getSession().getAttribute(MallConst.CURRENT_USER);
        if(user == null){
            log.info("user == null 拦截");
            // 抛出异常然后在异常中进行下一步处理
            throw new UserLoginException();
        }
        return true;
    }
}
